<?php

require_once 'AppConfig.php';
require_once 'Model/FileInfo.php';

/**
 * This Class is use for file Upload and Download
 */
class Controller_FileManagement {

    public $ControllerName = 'Controller_FileManagement';
    private $FILE_REQUEST;
    private $UserEntity;
    public $DbHelper = null;

    public function __construct() {
        $this->DbHelper = new DbHelper();
    }

    /**
     * Get all the File list
     * @param type $applicationId
     * @param type $handleSchoolId
     * @return type
     */
    public function getFileList($applicationId, $handleSchoolId) {
        $strSQL = "call Proc_Busi_GetAttachedFileList(?,?)";
        $parameters[] = $applicationId;
        $parameters[] = $handleSchoolId;
        $res = $this->DbHelper->execProcedures($strSQL, $parameters);
        return $res;
    }

    /**
     * Handle the Upload File
     */
    public function HandleUploadFile() {
        $this->FILE_REQUEST = $_FILES["fileToUpload"];
        $this->UserEntity = $_SESSION["UserEntity"];
        $fileinfo = new FileInfo();
        $fileinfo->TempPath = $this->FILE_REQUEST["tmp_name"];
        $fileinfo->FileName = $this->FILE_REQUEST["name"];
        $fileinfo->FileSize = $this->FILE_REQUEST["size"];
        $fileinfo->FileID = AppConfig::getUniqueId();
        $fileinfo->DestinationPath = AppConfig::getUpLoadPath() . $fileinfo->FileID;
        $fileinfo->StoreState = copy($fileinfo->TempPath, $fileinfo->DestinationPath);
        return $fileinfo;
    }

    public function HandleDownloadFile($FileID) {
        $format = "SELECT func_Busi_GetFileNameByID('%s') AS FileName";
        $strSQL = sprintf($format, $FileID);
        $res = $this->DbHelper->execSqlStatement($strSQL);

        if (empty($res)) {
            echo 'the file is not exist!';
            die();
        }

        $physicalPath = AppConfig::getUpLoadPath() . $FileID;
        $len = filesize($physicalPath);
        $outputFileName = $res[0]["FileName"];
        $extend = array_pop(explode(".", $outputFileName));
        $mineType = $this->getMine(strtolower($extend));

        if (!file_exists($physicalPath)) {
            echo 'File No Exist';
            die();
        }

        ob_end_clean();
        header("Pragma: public");
        header("Expires: 0"); // set expiration time  
        header("Cache-Component: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        header("Content-Type: $mineType");
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; Filename=' . $outputFileName);
        header('Content-Transfer-Encoding: binary');
        header("Content-Length: " . $len);
        readfile($physicalPath);
    }

    /**
     * Get the mine
     * @param type $extend
     * @return type
     */
    function getMine($extend) {
        //$fileList = AppConfig::getMineTypeList();
        //return $fileList[$extend];
        return 'application/octet-stream';
    }

}

?>
